<template>
	<view>
		<!-- <page-head title="登录"></page-head> -->
		<view class="uni-padding-wrap uni-common-mt">
			<form @submit="loginSubmit">
				<view class="uni-form-item uni-column">
					<span class="title">账号</span>
					<input class="uni-input" name="username" placeholder="请输入账号" />
				</view>
				<view class="uni-form-item uni-column">
					<span class="title">密码</span>
					<input class="uni-input" password name="password" placeholder="请输入密码" />
				</view>
				<view class="uni-btn-v">
					<button type="primary" form-type="submit">登录</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	var  graceChecker = require("@/common/graceChecker.js");
	import {setToken} from '@/utils/auth.js';
	import {login} from '@/api/login.js';
	export default {
		data() {
			return {
			}
		},
		onLoad() {
		},
		methods: {
			loginSubmit: function(e) {
                //定义表单规则
				var rule = [
				    {name:"username", checkType : "string", checkRule:"1,20",  errorMsg:"账号应为1-20个字符"},
				    {name:"password", checkType : "string", checkRule:"0,50",  errorMsg:"密码应为1-50个字符"},
				];
                //进行表单检查
                var formData = e.detail.value;
                var checkRes = graceChecker.check(formData, rule);
                if(checkRes){
					this.$store.dispatch('user/login', formData).then(() => {
						uni.switchTab({ url:'/pages/projectInfo/project-lists' })
					})
					.catch(error => {
						uni.showToast({
							icon:"none",
							title: error,
							duration: 1500
						});
					});
                }else{
                    uni.showToast({ title: graceChecker.error, icon: "none" });
                }
			}
		}
	}
</script>
<style>
</style>
